package com.digitalchina.lxnsy.activity.old;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPlan;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PlanVoSimpleVO;
import com.digitalchina.lxnsy.entity.ProductPlanListVO;
import com.digitalchina.lxnsy.fragment.PlanFarmingRecordFragment;
import com.digitalchina.lxnsy.fragment.PlantProductionFragment;
import com.digitalchina.lxnsy.fragment.PlantToadyRecommendFragment;
import com.digitalchina.lxnsy.toolkit.DateStyle;
import com.digitalchina.lxnsy.toolkit.DateUtil;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.kekstudio.dachshundtablayout.DachshundTabLayout;
import com.kekstudio.dachshundtablayout.indicators.LineMoveIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/8/23 14:34
 * 修改备注：
 * ================================================
 */
public class StandardProductionDetailActivity extends BaseActivity {

    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.layout_top_select)
    LinearLayout layoutTopSelect;
    @Bind(R.id.tab_layout)
    DachshundTabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.tv_time)
    TextView tvTime;


    private String title[] = {"今日推荐", "生产计划", "农事记录"};
    private List<Fragment> fragments = new ArrayList<>();
    private MyPagerAdapter adapter;

    private String planId = "";
    private String city = "";
    private List<PlanVoSimpleVO> planList = new ArrayList<>();

    private ProductPlanListVO vo = new ProductPlanListVO();
    //选择器
    private OptionsPickerView pvPlan;
    PlantToadyRecommendFragment fragment1;
    PlantProductionFragment fragment2;
    PlanFarmingRecordFragment fragment3;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_standard_production_detail);
        setTitle("标准化生产详情");
        setRightText("农事记账");
    }

    @Override
    public void initMember() {

        vo = (ProductPlanListVO) getIntent().getSerializableExtra("ProductPlanListVO");

        planId = vo.getId();

        tvName.setText("（" + DateUtil.getDate(vo.getSeedDate(), DateStyle.YYYY_MM_DD) + " 种植）\n" + vo.getCropType().getName());


        BusinessPlan.getPlanAllList(this, mHandler);


        tabLayout.setAnimatedIndicator(new LineMoveIndicator(tabLayout));

        fragment1 = new PlantToadyRecommendFragment();
        fragment2 = new PlantProductionFragment();
        fragment3 = new PlanFarmingRecordFragment();

        Bundle bundle = new Bundle();
        bundle.putString("planId", planId);
        if (vo.getProductStandard() != null) {
            if (vo.getProductStandard().getProvince() != null)
                city = vo.getProductStandard().getProvince().getName();
            if (vo.getProductStandard().getCity() != null)
                city = vo.getProductStandard().getCity().getName();
            if (vo.getProductStandard().getCounty() != null)
                city = vo.getProductStandard().getCounty().getName();
        }

        bundle.putString("city", city);
        bundle.putString("address", vo.getFullAddress());
        fragment1.setArguments(bundle);
        fragment2.setArguments(bundle);
        fragment3.setArguments(bundle);

        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);


        adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(title.length);
        viewPager.setAdapter(adapter);

        viewPager.setOffscreenPageLimit(title.length);

        tabLayout.setupWithViewPager(viewPager);


        pvPlan = new OptionsPickerView.Builder(StandardProductionDetailActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {

                String select = planList.get(options1).getCropName();
                planId = planList.get(options1).getId();

                tvName.setText("（" + DateUtil.getDate(planList.get(options1).getSeedDate(), DateStyle.YYYY_MM_DD) + " 种植）\n" + select);
                fragment1.updatePlanId(planId);
                fragment2.updatePlanId(planId);
                fragment3.updatePlanId(planId);
                setTopView(planList.get(options1).getIsFree(),planList.get(options1).getLeaveValidDay());
            }
        })
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.5f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.colorFontLightGrayB))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0)  //设置默认选中项
                .build();
    }

    @Override
    public void addListener() {
        layoutTopSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (planList != null && planList.size() > 0)
                    pvPlan.show();
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_GET_PLAN_ALL_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PLAN_LIST_FIELD:
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        planList = FastJsonUtil.getListBean(object.toString(), "planVoList", PlanVoSimpleVO.class);

        pvPlan.setPicker(planList);
    }

    public void setTopView(int isFree, int day) {
        if (isFree != 0) {
            if (day > 0)
                tvTime.setText("剩余有效期\n" + day + "天");
            else
                tvTime.setText("剩余有效期\n不足1天");
        }
    }

    @Override
    public void rightOnClick() {
        vo.setId(planId);
        GotoUtil.gotoActivity(this, KeepAccountsMainActivity.class, "ProductPlanListVO", vo);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        private MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

    }
}
